package com.evernote.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.evernote.entity.Content;
import com.evernote.entity.NoteBook;
import com.evernote.service.ContentService;
import com.evernote.service.NoteBookService;

/**
 * Servlet implementation class DoDeleteNotebook
 */
@WebServlet("/DoDeleteNotebook")
public class DoDeleteNotebook extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoDeleteNotebook() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		
		String msg = "";
		
		int Nid = Integer.parseInt(request.getParameter("Nid"));
		int Uid = Integer.parseInt(request.getParameter("Uid"));
		
		NoteBook n = new NoteBook();
		NoteBookService nbs = new NoteBookService();
		Content c = new Content();
		ContentService cs = new ContentService();
		
		n.setId(Nid);
		n.setUid(Uid);
		c.setNid(Nid);
		c.setUid(Uid);
		boolean isNotebookShow = false;
		if(nbs.DeleteNotebookById(n) != 0) {//删除笔记本
			cs.deleteNoteByNid(c);//删除笔记本所属笔记
			msg = "删除成功";
			isNotebookShow = true;
		} else {
			msg = "删除失败";
		}
		request.setAttribute("Uid", Uid);
		request.setAttribute("isNotebookShow", isNotebookShow);
		request.setAttribute("msg", msg);
		request.getRequestDispatcher("ToIndex").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
